from django.db import models
from django.contrib.auth.models import User
import uuid

class KnowledgeDocument(models.Model):
    """知识库文档模型"""
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=200, verbose_name='文档标题')
    content = models.TextField(verbose_name='文档内容')
    file_path = models.CharField(max_length=500, verbose_name='文件路径')
    file_type = models.CharField(max_length=50, verbose_name='文件类型')
    vector_id = models.CharField(max_length=100, blank=True, null=True, verbose_name='向量ID')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '知识库文档'
        verbose_name_plural = '知识库文档'
        ordering = ['-created_at']

    def __str__(self):
        return self.title

class InterviewSession(models.Model):
    """面试会话模型"""
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    session_name = models.CharField(max_length=200, verbose_name='会话名称')
    knowledge_documents = models.ManyToManyField(KnowledgeDocument, verbose_name='知识库文档')
    status = models.CharField(max_length=20, choices=[
        ('active', '进行中'),
        ('completed', '已完成'),
        ('paused', '暂停')
    ], default='active', verbose_name='状态')
    total_score = models.FloatField(default=0.0, verbose_name='总分')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '面试会话'
        verbose_name_plural = '面试会话'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username} - {self.session_name}"

class InterviewQuestion(models.Model):
    """面试问题模型"""
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    session = models.ForeignKey(InterviewSession, on_delete=models.CASCADE, verbose_name='面试会话')
    question_text = models.TextField(verbose_name='问题内容')
    question_type = models.CharField(max_length=50, choices=[
        ('technical', '技术问题'),
        ('behavioral', '行为问题'),
        ('general', '一般问题')
    ], default='technical', verbose_name='问题类型')
    source_document = models.ForeignKey(KnowledgeDocument, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='来源文档')
    order = models.IntegerField(default=0, verbose_name='问题顺序')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '面试问题'
        verbose_name_plural = '面试问题'
        ordering = ['order', 'created_at']

    def __str__(self):
        return f"{self.session.session_name} - 问题{self.order}"

class InterviewAnswer(models.Model):
    """面试答案模型"""
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    question = models.ForeignKey(InterviewQuestion, on_delete=models.CASCADE, verbose_name='问题')
    answer_text = models.TextField(verbose_name='答案内容')
    similarity_score = models.FloatField(default=0.0, verbose_name='相似度分数')
    quality_score = models.FloatField(default=0.0, verbose_name='质量分数')
    feedback = models.TextField(blank=True, verbose_name='反馈')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '面试答案'
        verbose_name_plural = '面试答案'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.question} - 答案"
